package com.cirkana.viewer.location;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.cirkana.CirkanaActivity;
import com.cirkana.serial.SerializationException;
import com.cirkana.viewer.R;

public class ChangeProviderDialog extends CirkanaActivity {

	public static final int RESULT_CHANGEPROVIDER_OK = 0;
	
	private ChangeProviderPassData data;

	private LinearLayout layout;

	private List<String> locationProviders;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.changeprovider);

		this.data = (ChangeProviderPassData) getRequest(ChangeProviderPassData.class);

		layout = (LinearLayout) findViewById(R.id.changeprovider_linearLayout);

		this.locationProviders = MapLocation.getLocationProviders();
		int iii = 0;
		for (String provider : this.locationProviders) {
			RadioButton radioButton = new RadioButton(layout.getContext());
			radioButton.setText(provider);
			if (this.data.getLocationProvider().equals(provider))
				radioButton.setChecked(true);
			radioButton.setTextColor(R.color.cirkanaBlack);
			radioButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View selected) {
					data.setLocationProvider(((RadioButton) selected).getText()
							.toString());
					try {
						setResponse(RESULT_CHANGEPROVIDER_OK, data);
					} catch (SerializationException e) {
						openMessageBox("Error", e.getMessage());
					}
					finish();
				}
			});
			layout.addView(radioButton, iii++);
		}
	}

}
